package com.lefeng.cockpit.admin.api.v1.resource;

import com.didiglobal.logi.security.common.PagingData;
import com.didiglobal.logi.security.common.PagingResult;
import com.didiglobal.logi.security.common.Result;
import com.google.protobuf.ServiceException;
import com.lefeng.cockpit.admin.util.ParameterUtils;
import com.lefeng.cockpit.common.bean.dto.resource.ResourceDTO;
import com.lefeng.cockpit.common.bean.po.resource.ResourcePO;
import com.lefeng.cockpit.resource.service.ResourcesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import static com.lefeng.cockpit.common.enums.resource.Status.RESOURCE_FILE_IS_EMPTY;


/**
 * resources controller
 */
@Api(tags = "ResourcePO-资源相关接口")
@RestController
@Slf4j
@RequestMapping("/api/v1/resources")
public class ResourcesController {

    @Resource
    private ResourcesService resourceService;

    /**
     * @return create  result code
     */
    @ApiOperation(value = "createDirectory", notes = "CREATE_RESOURCE_NOTES")
    @PostMapping(value = "/directory")
    public Result<Object> createDirectory(@RequestBody @Validated ResourceDTO dto) throws ServiceException {
        return resourceService.createDirectory(dto);
    }

    /**
     * create resource
     *
     * @return create result code
     */
    @ApiOperation(value = "createResource", notes = "CREATE_RESOURCE_NOTES")
    @PostMapping()
    public Result<Object> createResource(@RequestBody @Validated ResourceDTO dto) throws ServiceException {
        return resourceService.createResource(dto);
    }

    /**
     * create resource file online
     *
     * @return create result code
     */
    @ApiOperation(value = "onlineCreateResource", notes = "ONLINE_CREATE_RESOURCE_NOTES")
    @PostMapping(value = "/online-create")
    public Result onlineCreateResource(@RequestBody @Validated ResourceDTO dto) throws ServiceException {
        if (StringUtils.isEmpty(dto.getContent())) {
            log.error("resource file contents are not allowed to be empty");
            return Result.buildParamIllegal(RESOURCE_FILE_IS_EMPTY.getMsg());
        }
        return resourceService.onlineCreateResource(dto);
    }

    /**
     * edit resource file online
     *
     * @return update result code
     */
    @ApiOperation(value = "updateResourceContent", notes = "UPDATE_RESOURCE_NOTES")
    @PutMapping(value = "/update-content")

    public Result updateResourceContent(@RequestBody @Validated ResourceDTO dto) throws ServiceException {
        if (StringUtils.isEmpty(dto.getContent())) {
            log.error("The resource file contents are not allowed to be empty");
            return Result.buildParamIllegal(RESOURCE_FILE_IS_EMPTY.getMsg());
        }
        return resourceService.updateResourceContent(dto);
    }

    /**
     * delete resource
     *
     * @return delete result code
     */
    @ApiOperation(value = "deleteResource", notes = "DELETE_RESOURCE_BY_ID_NOTES")
    @DeleteMapping(value = "/{id}")
    @ResponseStatus(HttpStatus.OK)
    public Result<Object> deleteResource(
            @PathVariable(value = "id") int resourceId) throws Exception {
        return resourceService.delete(resourceId);
    }

    /**
     * query resources list paging
     *
     * @return resource list page
     */
    @ApiOperation(value = "queryResourceListPagi, ng", notes = "QUERY_RESOURCE_LIST_PAGING_NOTES")
    @PostMapping("/page")
    @ResponseStatus(HttpStatus.OK)
    public PagingResult<ResourcePO> queryResourceListPaging(@RequestBody @Validated ResourceDTO dto) throws ServiceException {
        String searchVal = ParameterUtils.handleEscapes(dto.getSearchVal());
        dto.setSearchVal(searchVal);
        PagingData<ResourcePO> result = resourceService.queryResourceListPaging(dto);
        return PagingResult.success(result);
    }

    /**
     * view resource file online
     *
     * @param resourceId  resource id
     * @param skipLineNum skip line number
     * @param limit       limit
     * @return resource content
     */
    @ApiOperation(value = "viewResource", notes = "VIEW_RESOURCE_BY_ID_NOTES")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "RESOURCE_ID", required = true, dataTypeClass = int.class, example = "100"),
            @ApiImplicitParam(name = "skipLineNum", value = "SKIP_LINE_NUM", required = true, dataTypeClass = int.class, example = "100"),
            @ApiImplicitParam(name = "limit", value = "LIMIT", required = true, dataTypeClass = int.class, example = "100")
    })
    @GetMapping(value = "/view/{id}")
    public Result viewResource(
            @PathVariable(value = "id") int resourceId,
            @RequestParam(value = "skipLineNum") int skipLineNum,
            @RequestParam(value = "limit") int limit) {
        return resourceService.readResource(resourceId, skipLineNum, limit);
    }

    /**
     * view resource file online
     *
     * @return resource content
     */
    @ApiOperation(value = "viewResource", notes = "VIEW_RESOURCE_BY_ID_NOTES")
    @GetMapping(value = "/tree")
    public Result getTree() {
        return resourceService.getTree();
    }


}
